package com.example.ssm.controller;

import com.example.ssm.context.PaymentContext;
import com.example.ssm.enums.PaymentType;
import com.example.ssm.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付控制器
 * 提供支付相关的API接口
 */
@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;
    
    @Autowired
    private PaymentContext paymentContext;

    /**
     * 处理支付请求
     * @param paymentType 支付类型代码
     * @param amount 支付金额
     * @param orderId 订单ID
     * @return 支付结果
     */
    @PostMapping("/pay")
    public Map<String, Object> pay(
            @RequestParam String paymentType,
            @RequestParam BigDecimal amount,
            @RequestParam String orderId) {

        String result = paymentService.processPayment(paymentType, amount, orderId);

        Map<String, Object> response = new HashMap<>();
        response.put("success", !result.contains("失败") && !result.contains("不支持"));
        response.put("message", result);
        response.put("orderId", orderId);
        response.put("amount", amount);
        response.put("paymentType", paymentType);

        return response;
    }

    /**
     * 获取所有支付方式
     * @return 支付方式列表
     */
    @GetMapping("/types")
    public Map<String, Object> getPaymentTypes() {
        Map<String, Object> response = new HashMap<>();
        Map<String, String> types = new HashMap<>();

        for (PaymentType type : PaymentType.values()) {
            types.put(type.getCode(), type.getDesc());
        }

        response.put("success", true);
        response.put("paymentTypes", types);

        return response;
    }

    /**
     * 使用枚举名称直接支付（演示另一种使用方式）
     * @param paymentTypeName 支付类型枚举名称
     * @param amount 支付金额
     * @param orderId 订单ID
     * @return 支付结果
     */
    @PostMapping("/pay-by-enum")
    public Map<String, Object> payByEnum(
            @RequestParam String paymentTypeName,
            @RequestParam BigDecimal amount,
            @RequestParam String orderId) {

        Map<String, Object> response = new HashMap<>();

        try {
            PaymentType paymentType = PaymentType.valueOf(paymentTypeName.toUpperCase());
            String result = paymentService.processPayment(paymentType, amount, orderId);

            response.put("success", !result.contains("失败"));
            response.put("message", result);
            response.put("orderId", orderId);
            response.put("amount", amount);
            response.put("paymentType", paymentType.getDesc());

        } catch (IllegalArgumentException e) {
            response.put("success", false);
            response.put("message", "不支持的支付方式: " + paymentTypeName);
        }

        return response;
    }
    
    /**
     * 根据支付账号自动识别支付方式并处理支付
     * @param paymentAccount 支付账号（支付宝账号、微信支付标识或银行卡号）
     * @param amount 支付金额
     * @param orderId 订单ID
     * @return 支付结果
     */
    @PostMapping("/auto-pay")
    public Map<String, Object> autoPay(
            @RequestParam String paymentAccount,
            @RequestParam BigDecimal amount,
            @RequestParam String orderId) {
        
        String result = paymentContext.processPayment(paymentAccount, amount, orderId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", !result.contains("失败") && !result.contains("无法识别"));
        response.put("message", result);
        response.put("orderId", orderId);
        response.put("amount", amount);
        response.put("paymentAccount", paymentAccount);
        
        return response;
    }
}
